<?php
$db = new Database();
if(isset($_POST['action']))
{
    $auth = Auth::getAuth('current_user');
    $userID = $auth['id'];
    if($_POST['action']=="Product"){
        // Path Location of the file to be uploaded in the system.
        $locFile = $_POST['locFile'];
        $destdir = $locFile;
        //check if folder location is empty
        $handle = opendir($destdir);
        $c = 0;
        while ($file = readdir($handle)&& $c<3)
        {
            $c++;
        }
        if ($c>2)
        {
            $dirs = scandir($locFile);
            foreach($dirs as $filename)
            {
                if (($filename == '.')||($filename == '..'))
                {
                }elseif (is_dir($locFile.$filename))
                {}else
                {
                    $extension = explode(".", $filename);
                    // Determining if file is already exist or not
                    $getfilename = $db->query("SELECT * FROM tbfname WHERE filename='$filename'","array");
                    foreach($getfilename as $dataFname){
                        $dbFname = $dataFname['filename'];
                    }
                    if($dbFname==$filename){
                        $varAlert = 4;
                    }else{
                        if($extension[1]=="csv")
                        {
                            $location = $locFile.$filename;
                            $loop = 0;
                            $fp = @fopen($location,'r');
                                while (!feof($fp))
                                {
                                    $dest = $_POST['location'];
                                    $a = 1;
                                        while (($data = fgetcsv($fp, 1000, ",")) !== FALSE){
                                            $num = count($data);// Count number of Column
                                            // Validating if one date format is correct 
                                            $dateFormatFrom = explode("/", $data[5]);
                                            $dateFormatTo = explode("/", $data[6]);
                                            $formatFrom = $dateFormatFrom['1'] ."/". $dateFormatFrom['0'] ."/". $dateFormatFrom['2'];
                                            $formatTo = $dateFormatTo['1'] ."/". $dateFormatTo['0'] ."/". $dateFormatTo['2'];
                                            $reg="#^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$#";
                                            $dateTime = date("Y-m-d  H:i:s");
                                            
                                            // Product Line
                                            $plineSplit = explode("-", $data[2]);
                                            
                                            
                                            if(!preg_match($reg, $formatFrom)||!preg_match($reg, $formatTo)){
                                                $insert = array("user_id"=>$userID,
                                                                        "action_id"=>'20',
                                                                        "filename"=>$filename,
                                                                        "type"=>$a,
                                                                        "datetime"=>$dateTime,
                                                                        "is_active"=>'1'
                                                                        );
                                                $db->insert('tbaudit_trail_uploading',$insert);
                                                $varAlert = 5;
                                            // Validating if one row/column is null
                                            }elseif($data[0]==""||$data[1]==""||$data[2]==""||$data[3]==""||$data[4]==""||$data[5]==""||$data[6]==""){
                                                $insert = array("user_id"=>$userID,
                                                                "action_id"=>'21',
                                                                "filename"=>$filename,
                                                                "type"=>$a,
                                                                "datetime"=>$dateTime,
                                                                "is_active"=>'1'
                                                                );
                                                $db->insert('tbaudit_trail_uploading',$insert);
                                                $varAlert = 6;
                                            }elseif($num!=7){
                                                $varAlert = 7;
                                            }elseif($plineSplit[0]=="PLINE"){
                                                $code = $data[3];
                                                $pline = $db->query("SELECT * FROM tbproduct_line WHERE code='$code'","numrows");
                                                if (strpos($pline,'0') !== false)
                                                {
                                                    $insert = array("user_id"=>$userID,
                                                                    "action_id"=>23,
                                                                    "type"=>$a,
                                                                    "filename"=>$filename,
                                                                    "datetime"=>$dateTime,
                                                                    "is_active"=>'1'
                                                                    );
                                                    $db->insert('tbaudit_trail_uploading',$insert);
                                                    $varAlert = 8;
                                                }
                                            }
                                            $a++;
                                        }
                                    
                                    $fp++;
                                }
                            fclose($fp);
                        }
                        else
                        {
                            $varAlert = 3;
                        }
                    }
                }
            }
        }
        else
        {
            $varAlert = 2;
        }
    }elseif($_POST['action']=="RewardPoints"){
        // Path Location of the file to be uploaded in the system.
        $locFile = $_POST['locFile'];
        $destdir = $locFile;
        //check if folder location is empty
        $handle = opendir($destdir);
        $c = 0;
        while ($file = readdir($handle)&& $c<3)
        {
            $c++;
        }
        if ($c>2)
        {
            $dirs = scandir($locFile);
            foreach($dirs as $filename)
            {
                if (($filename == '.')||($filename == '..'))
                {
                }elseif (is_dir($locFile.$filename))
                {}else
                {
                    $extension = explode(".", $filename);
                    // Determining if file is already exist or not
                    $getfilename = $db->query("SELECT * FROM tbfname WHERE filename='$filename'","array");
                    foreach($getfilename as $dataFname){
                        $dbFname = $dataFname['filename'];
                    }
                    if($dbFname==$filename){
                        $varAlert = 4;
                    }else{
                        if($extension[1]=="csv")
                        {
                            $location = $locFile.$filename;
                            $loop = 0;
                            $fp = @fopen($location,'r');
                                while (!feof($fp))
                                {
                                    $dest = $_POST['location'];
                                    $a = 1;
                                        while (($data = fgetcsv($fp, 1000, ",")) !== FALSE){
                                            $num = count($data);// Count number of Column
                                            // Validating if one date format is correct (mm/dd/yyyy) 
                                            
                                            $dateTime = date("Y-m-d  H:i:s");
                                            if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $data[0])){
                                                $insert = array("user_id"=>$userID,
                                                                        "action_id"=>'20',
                                                                        "filename"=>$filename,
                                                                        "type"=>$a,
                                                                        "datetime"=>$dateTime,
                                                                        "is_active"=>'1'
                                                                        );
                                                $db->insert('tbaudit_trail_uploading',$insert);
                                                $varAlert = 5;
                                            // Validating if one row/column is null
                                            }elseif($data[0]==""||$data[1]==""||$data[2]==""||$data[3]==""||$data[4]==""||$data[5]==""||$data[6]==""){
                                                $insert = array("user_id"=>$userID,
                                                                "action_id"=>'21',
                                                                "filename"=>$filename,
                                                                "type"=>$a,
                                                                "datetime"=>$dateTime,
                                                                "is_active"=>'1'
                                                                );
                                                $db->insert('tbaudit_trail_uploading',$insert);
                                                $varAlert = 6;
                                            }elseif($num!=7){
                                                $varAlert = 7;
                                            }
                                            $a++;
                                        }
                                    $fp++;
                                }
                            fclose($fp);
                        }
                        else
                        {
                            $varAlert = 3;
                        }
                    }
                }
            }
        }
        else
        {
            $varAlert = 2;
        }
    }
}
if($varAlert==5||$varAlert==6){
    echo "There was an Error while uploading your file.";
}elseif($varAlert==2){
    echo "Folder location is empty.";
}elseif($varAlert==3){
    echo "Invalid File Format.";
}elseif($varAlert==4){
    echo "File is already uploaded.";
}elseif($varAlert==8){
    
    echo "PLINE type doesnt match on our record.";
}elseif($varAlert==7){
    $insert = array("user_id"=>$userID,
                    "action_id"=>'22',
                    "filename"=>$filename,
                    "datetime"=>$dateTime,
                    "is_active"=>'1'
                    );
    $db->insert('tbaudit_trail_uploading',$insert);
    echo "There was an Error while uploading your file.";
}